import 'package:get/get.dart';
import 'package:razer_one/app/main/model/eventbus_model.dart';
import 'package:razer_one/app/main/model/status_model.dart';
import 'package:razer_one/app/main/utils/app_manager.dart';
import 'package:razer_one/app/modules/commond/test_page.dart';
import 'package:razer_one/app/modules/home/home/home_model.dart';
import 'package:razer_one/app/routes/app_routes.dart';

class HomeController extends GetxController {
  var dataArr = <HomeModel>[];
  var statusModel = StatusModel(isShowLoadingView: true);
  final _slideModel = SlideCellModel(index: 0);
  @override
  void onInit() {
    super.onInit();
    loadData();
  }

  loadData() {
    Future.delayed(const Duration(milliseconds: 500), () {
      for (var i = 0; i < 20; i++) {
        HomeModel model = HomeModel(title: "$i", isOpen: false);
        dataArr.add(model);
      }
      statusModel.isShowLoadingView = false;

      update();
    });
  }

  //网络错误
  netErrorHandle() {}
  //下拉刷新
  downRefreshHandle() {
    Future.delayed(const Duration(milliseconds: 500), () {
      statusModel.isDownRefreshing = false;

      update();
    });
  }

  //上拉刷新
  upRefreshHandle() {
    Future.delayed(const Duration(milliseconds: 500), () {
      for (var i = 0; i < 10; i++) {
        HomeModel model = HomeModel(title: "$i", isOpen: false);
        dataArr.add(model);
      }
      statusModel.isLoading = false;

      update();
    });
  }

  //空数据按钮点击
  emptyBtnHandle() {
    Get.toNamed(AppRoutes.homeAddRoute);
  }

  //导航栏头像点击
  headBtnClick() {
    Get.to(const DeletePage());
  }

  //导航栏添加按钮点击
  addBtnClick() {
    Get.toNamed(AppRoutes.homeAddRoute);
  }

  //cell点击
  didSelectItemAtIndex(int index) {
    dataArr[index].isOpen = !dataArr[index].isOpen;
    update();
  }

  //cell删除按钮
  deleteItemAtIndex(int index) {
    dataArr.removeAt(index);
    update();
    _slideModel.index = -1;
    AppManager.instance.eventBus?.fire(_slideModel);
  }

  //手势代理回调
  onSlideStarted(int index) {
    _slideModel.index = index;
    AppManager.instance.eventBus?.fire(_slideModel);
  }

  onSlideCanceled(int index) {
    _slideModel.index = index;
  }

  onHorizontalDragDown() {
    _slideModel.index = -1;
    AppManager.instance.eventBus?.fire(_slideModel);
  }
}
